import React from 'react'
// ===================================================================== Temp
import SearchForm from '#cpt/temp/SearchForm'
import Table from '#cpt/temp/Table'
import TableControls from '#cpt/temp/TableControls'
import FormModal from '#cpt/temp/FormModal'
// ===================================================================== global declare
const { $fn, $Lazy } = window
// ===================================================================== Lazy
const Page 		= $Lazy.load(()=>import('#tp/page/page-container'))
const $cache 	= $Lazy.file(()=>import('#private/cache-api'))
// ===================================================================== data
const StatusData = [{ name:'全部', value: '' }, { name:'草稿', value: '0' },{ name:'正常', value: '1' }, { name:'已过期', value: '2' }]
const AuditStatus = [{ name:'全部', value: '' },{ name:'待审核', value: '0' }, { name:'审核成功', value: '1' }]
// ===================================================================== component
class Index extends React.Component{
	state = {
		data:[],
		pag: {},
		// 搜索表单列表
		forms : [
			{ label: '合同编号',		name: 'code' },
			{ label: '合同名称',		name: 'name' },
			{ label: '客户名称',		name: 'hosp_id',	type: 'select', loading:true,nameStr:'name' },
			{ label: '合同失效',		name: 'invalid',	type: 'select', data:[{ name:'7天内', value: '7' },{ name:'15天内', value: '15' }, { name:'1个月内', value: '31' }] },
			{ label: '到期时间',		names: ['start_at','end_at'],	type: 'date-range' },
			{ label: '合同状态',		name: 'status',		type: 'select',idStr:'value',nameStr:'name',data:StatusData },
			{ label: '审核状态',		name: 'enabled',	type: 'select',idStr:'value',nameStr:'name',data:AuditStatus },
		],
		// 提交表单列表
		formData : [
			{ label: '合同编号',		name: 'code', 				half:true,  disabled:true },
			{ label: '合同名称',		name: 'name', 				half:true,  disabled:true },
			{ label: '折扣率',		name: 'discount_rate', 		half:true },
			{ label: '延期时间',		name: 'delay', 				half:true, 	required:true, type:'number', afterText:'月'},
			{ label: '新生效时间',	name: 'sign_at', 			half:true,  required:true, type:'date-time', onValue:({ value, formData })=>{
				$fn.getRow(formData,'contract_validity').before = value
				this.setState({ formData })
			}},
			{ label: '新到期时间',	name: 'contract_validity', 	half:true, 	required:true, type:'date-time', onValue:({ value, formData })=>{
				$fn.getRow(formData,'sign_at').after = value
				this.setState({ formData })
			}},
		]
	}
	
	model = {}
	query = { type:'my' }
	
	componentDidMount(){
		const { type } = this.props
		if(type){ this.query = { type } }
		
		this.getSelect()
		
		if(window.isInit){
			this.fetch()
		}
		
		$fn.fresh.on(this.fetch)
	}
	
	componentWillUnmount(){
		$fn.fresh.remove()
	}
	// 获取下拉数据
	getSelect = async () => {
		const { forms } = this.state
		$cache.then( async f => {
			// 客户名称
			$fn.setRow(forms, 'hosp_id', await f.getCache({ api:'hospital' } ) )
			this.setState({ forms })
		})
		
	}
	
	// paging
	fetch = () => $fn.fetch.call(this,'bs-contract/business', { isIndex:1 })
	
	// 表单
	cols = [
		{ title: '合同编号', 		dataIndex: 'code', 				width:100 },
		{ title: '合同名称', 		dataIndex: 'name', 				width:200 },
		{ title: '客户名称', 		dataIndex: 'hosp_name', 		width:200 },
		{ title: '业务员名称', 		dataIndex: 'salesman_user', 	...$fn.table.name},
		{ title: '生效时间', 		dataIndex: 'sign_at', 			...$fn.table.time },
		{ title: '到期时间', 		dataIndex: 'contract_validity', ...$fn.table.time },
		{ title: '延期时间(月)', 	dataIndex: 'delay', 			width:100, align:'center'},
		{ title: '合同状态', 		dataIndex: 'status', 			width:100,  render: text => $fn.getState(StatusData, text) },
		{ title: '审核状态', 		dataIndex: 'enabled', 			width:100, render: text => +text === 1 ? '审核成功' : '待审核'  },
		{ title: '创建人（系统）', 	dataIndex: 'empl_name', 		width:150 },
		TableControls.controls.call(this, { width: 300 })
	]
	
	// 表格操作按钮
	table_controls = (text, rows) => {
		const { uuid, code } = rows
		return [
			TableControls.pushTo.call(this, '编辑',  `edit?uuid=${uuid}&edit=1` ),
			TableControls.pushTo.call(this, '查看',  `edit?uuid=${uuid}` ),
			TableControls.click.call(this, '续签', ()=> {
				window.$http.submit(this,'bs-contract/info', { param:{uuid}, isPull:true}).then(data=>{
					const { code, name, delay, discount_rate } = data
					this.submit = { uuid }
					this.modal.open({formRef: this.formRef})
					this.formRef.setValue({code, name, delay, discount_rate})
				})
			} ),
			TableControls.pushTo.call(this,'合同签订记录', `contract-record?code=${code}`)
		]
	}
	
	render(){
		const { title } = this.props
		return (
			<>
				<Page title={ title ? title : '我的合同管理'} >
					{/* 搜索 */}
					{ SearchForm.call(this) }
					{/* 表格 */}
					{ Table.call(this, { isIndex:1, isInit: window.isInit }) }
				</Page>
				{/* 合同续签 */}
				{ FormModal.call(this,{ title:'合同续签', labelWidth:'s9', width:800, api: 'bs-contract/renew'}) }
			</>
		)
	}
}

export default Index